package com.highveldsky.leaverequest.client.ui;

import java.util.Date;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.highveldsky.leaverequest.client.service.LeaveRequestServiceAsync;
import com.highveldsky.leaverequest.shared.model.LeaveRequest;

public class ApproveLeaveRequestsPanel extends FlowPanel {

	private LeaveRequestServiceAsync lrService;
	private List<LeaveRequest> leaveRequests;
	private Grid grid = new Grid();
	public String approverName;
	public DateTimeFormat dateFormat = DateTimeFormat.getFormat(PredefinedFormat.DATE_LONG);
	
	public ApproveLeaveRequestsPanel(LeaveRequestServiceAsync lrService) {
		this.lrService = lrService;
	}

	@Override
	protected void onLoad() {
		grid.addStyleName("leave-request-table");
		this.add(grid);
		try {
			lrService.listApproversLRs(approverName, new AsyncCallback<List<LeaveRequest>>() {

				@Override
				public void onSuccess(List<LeaveRequest> result) {
					leaveRequests = result;
					grid.resize(leaveRequests.size()+1, 8);
					addHeaderRow(0, "ID");
					addHeaderRow(1, "Employee");
					addHeaderRow(2, "Approver");
					addHeaderRow(3, "Approver Email");
					addHeaderRow(4, "First Leave Day");
					addHeaderRow(5, "Last Leave Day");
					addHeaderRow(6, "Approval Status");
					addHeaderRow(7, "Action");
					for (int i=1; i <= leaveRequests.size(); i++) {
						LeaveRequest lr = leaveRequests.get(i);
						addDataRow(i, 0, Long.toString(lr.id));
						addDataRow(i, 1, lr.employeeName);
						addDataRow(i, 2, lr.approverName);
						addDataRow(i, 3, lr.approverEmail);
						addDataRow(i, 4, lr.firstDay);
						addDataRow(i, 5, lr.lastDay);
						addDataRow(i, 6, "todo");
						Anchor approve = new Anchor("Approve");
						approve.addClickHandler(new ApproveLeaveRequest(lr.id));
						addDataRow(i, 7, approve);
					}
				}
				
				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					
				}
				
			});
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		
	}

	private void addHeaderRow(int column, String title) {
		HTML html = new HTML(title);
		html.addStyleName("th");
		grid.setWidget(0, column, html);
	}
	
	private void addDataRow(int row, int column, Widget widget) {
		grid.setWidget(row, column, widget);
		if (row % 2 != 0) {
			widget.addStyleName("odd-row");
		}
	}

	private void addDataRow(int row, int column, String content) {
		HTML html = new HTML(content);
		grid.setWidget(row, column, html);
		if (row % 2 != 0) {
			html.addStyleName("odd-row");
		}
	}

	private void addDataRow(int row, int column, Date content) {
		HTML html = new HTML(dateFormat.format(content));
		grid.setWidget(row, column, html);
		if (row % 2 != 0) {
			html.addStyleName("odd-row");
		}		
	}

	class ApproveLeaveRequest implements ClickHandler {

		long leaveRequestId;
		public ApproveLeaveRequest(long leaveRequestId) {
			this.leaveRequestId = leaveRequestId;
		}

		@Override
		public void onClick(ClickEvent event) {
			try {
				lrService.approveLR(leaveRequestId, new AsyncCallback<Boolean>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onSuccess(Boolean result) {
						// TODO Auto-generated method stub
						
					}
				});
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}

}
